package vn.edu.saigontech.ncms.web.study;

import java.io.IOException;
import java.util.Collection;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.ImportExport;
import vn.edu.saigontech.ncms.vo.FinalTestResultReportItem;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class FinalTestResultAction extends BaseAction {
	
	private Long offeringId;

	public String list() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		REQUEST().setAttribute("offeringMembers", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		return SUCCESS;
	}

	public String report() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("offeringMembers", OfferingMemberService.INSTANCE.findPassedByOfferingId(this.offeringId));
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.result.report", offering.getName());
		return SUCCESS;
	}
	
	public String reportAll() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("offeringMembers", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId, true));
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.result.reportAll", offering.getName());
		return SUCCESS;
	}
	
	public String doFinalTestResultChart() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		try {
			Collection<FinalTestResultReportItem> reportItems = OfferingMemberService.INSTANCE.reportFinalTestResult(this.offeringId);
			RESPONSE().setContentType("application/pdf");
			RESPONSE().setHeader("Content-disposition","attachment; filename=\""+ "Class" + this.offeringId +".pdf\"");
			ImportExport.exportToPdfStream("FinalTestResultChart.jasper", null, reportItems, RESPONSE().getOutputStream());
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		this.addLoggingDetail(BaseAction.REPORT, "logging.chart.report", offering.getName());
		return NONE;
	}
	
	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}
}
